package com.ms.service.controller;

import com.ms.service.entity.Product;
import com.ms.service.entity.ShoppingCartRecord;
import com.ms.service.service.ProductService;
import com.ms.service.service.ShoppingCartService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qihao on 2018/3/29.
 */
@RestController
public class ShoppingCartController {

    @Autowired
    private  ShoppingCartService shoppingCartService;

    @Autowired
    private ProductService productService;

    @RequestMapping(name = "initShoppinCart",value="/initShoppinCart/{userId}", method = RequestMethod.GET)
    public List<SCItem> initShoppinCart(@PathVariable("userId")String userId){
        List<ShoppingCartRecord> scrList = shoppingCartService.initShoppinCart(Integer.parseInt(userId));
        List<SCItem> result=new ArrayList<SCItem>();
        for (int i= 0;i<scrList.size();i++){
            ShoppingCartRecord scr = scrList.get(i);
            Product p =productService.getProduct(scr.getProductId());
            result.add(new SCItem(p,scr.getTotal()));
        }
        return result;
    }
    @RequestMapping(name = "addProductToSC",value="/addProductToSC/{userId}/{productId}", method = RequestMethod.GET)
    public boolean addProductToSC(@PathVariable("userId")String userId,@PathVariable("productId")String productId){
        ShoppingCartRecord scr = new ShoppingCartRecord();
        scr.setUserId(Integer.parseInt(userId));
        scr.setProductId(Integer.parseInt(productId));
        return shoppingCartService.addProductToSC(scr);
    }
    @RequestMapping(name = "removeOneProductfromSC",value="/removeOneProductfromSC/{userId}/{productId}", method = RequestMethod.GET)
    public boolean removeOneProductfromSC(@PathVariable("userId")String userId,@PathVariable("productId")String productId){
        ShoppingCartRecord scr = new ShoppingCartRecord();
        scr.setUserId(Integer.parseInt(userId));
        scr.setProductId(Integer.parseInt(productId));
        return shoppingCartService.removeOneProductfromSC(scr);
    }
    @RequestMapping(name = "removeAllProductfromSC",value="/removeAllProductfromSC/{userId}/{productId}", method = RequestMethod.GET)
    public boolean removeAllProductfromSC(@PathVariable("userId")String userId,@PathVariable("productId")String productId){
        ShoppingCartRecord scr = new ShoppingCartRecord();
        scr.setUserId(Integer.parseInt(userId));
        scr.setProductId(Integer.parseInt(productId));
        return shoppingCartService.removeAllProductsfromSC(scr);
    }
}
@Data
class SCItem{
    private Product product;
    private int total;

    public SCItem(Product product, int total) {
        this.product = product;
        this.total = total;
    }

    public SCItem() {
    }
}